<?php

namespace App\Models\Common;

use App\Models\Base;
use App\Models\House\HouseAttributeValue;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;

class CommonParam extends Base
{
    use ModelTree, AdminBuilder;
    //

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setParentColumn('parent_id');
        $this->setOrderColumn('sort');
        $this->setTitleColumn('name');
    }


    public static function treeData()
    {
        $data = CommonParam::query()->get()->toArray();
        $list = list_tree_Intelligence($data,'id','parent_id');

//        $last_names = array_column($list,'sort');
//        array_multisort($last_names,SORT_ASC,$list);
        $optionsParent =getChidldata($list);
        $optionsParent[0] = '顶级';

        $oparr = [];
        $oparr[0] =   '顶级';
        foreach ($optionsParent as $k=>$v)
        {
            $oparr[$k] =  $v;
        }

        return $oparr;
    }


    # 房屋属性 值
    public function HouseAttrVal()
    {
        return $this->hasMany(HouseAttributeValue::class,'value_param_id');
    }

    #  属性 单个
    public function HouseAttrone()
    {
        return $this->hasOne(HouseAttributeValue::class,'attribute_param_id');
    }

    # 子集关系
    public function childParam()
    {
        return $this->hasMany(CommonParam::class,'parent_id','id');
    }
}
